import {
  Controller,
  Get,
  Body,
  Post,
  HttpException,
  NotFoundException,
  HttpStatus,
  UseFilters, UsePipes,
} from '@nestjs/common';
import { CreateCatDto } from '../DTO/create-cat.dto';
import { CatsService } from '../providers/cats.service';
import { Cat } from '../interfaces/cat.interface';
import { JoiValidationPipe } from '../pipes/JoiValidationPipe';
import { ValidationPipe } from '../pipes/validation.pipe2';
import CatSchema from '../schema/cat.schema';

// Nest使用依赖注入的设计模式，这里是因为TS的特性，类型实现DI
// 默认使用构造函数的注入，也可以使用基于属性的注入
// CatsController是catsService的消费者
@Controller('cats')
export class CatsController {
  constructor(private catsService: CatsService) {}

  @Post('/add')
  @UsePipes(new JoiValidationPipe(CatSchema))
  async create(@Body() createCatDto: CreateCatDto) {
    console.log(createCatDto);
    this.catsService.create(createCatDto);
  }

  // 使用class validator
  @Post('/add1')
  async create1(@Body(new ValidationPipe()) createCatDto: CreateCatDto) {
    console.log('....')
    console.log(createCatDto);
    this.catsService.create(createCatDto);
  }


  @Get('/all')
  async findAll(): Promise<Cat[]> {
    return this.catsService.findAll();
  }

  @Get('/admin')
  async gundan(): Promise<Cat[]> {
    throw new NotFoundException();
    // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
  }
}
